package com.example.idachuappone.utils;

import org.json.JSONObject;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import com.example.idachuappone.index.welcomentity.Version;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class CheckVersionUpdate {

	class AnonymousClass_1 extends RequestCallBack<String> {
		private final/* synthetic */Context val$context;
		private final/* synthetic */boolean val$isAuto;

		AnonymousClass_1(boolean z, Context context) {
			this.val$isAuto = z;
			this.val$context = context;
		}

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			try {
				JSONObject obj = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
				String newVer = obj.optString("code");
				String size = obj.optString("size");
				String info = obj.optString("info");
				String optString = obj.optString("url");
				String[] infos = info.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("\u5927\u5c0f" + size + "M\u3001" + "\u66f4\u65b0\u5185\u5bb9:");
				for (String str : infos) {
					sb.append("\n\n");
					sb.append(str);
				}
				new Builder(this.val$context).setTitle("\u6709\u65b0\u7248\u53ef\u4ee5\u66f4\u65b0v" + newVer)
						.setMessage(sb.toString()).setPositiveButton("\u4e0b\u8f7d\u65b0\u7248", new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
							}
						}).setNegativeButton("\u4ee5\u540e\u518d\u8bf4", new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
							}
						}).setCancelable(false).show();
			} catch (Exception e) {
			}
		}

		@Override
		public void onFailure(HttpException arg0, String arg1) {
			if (!this.val$isAuto) {
				MainToast.show(this.val$context, "\u5f53\u524d\u4e3a\u6700\u65b0\u7248", 0);
			}
		}
	}

	public void checkVersion(Context context, boolean isAuto) {
		NetUtil.get(context, "", 100, new AnonymousClass_1(isAuto, context));
	}

	public boolean isYesUpdate(Context context, Version version) {
		return getCode(getVersion(context)) < getCode(version.getMin());
	}

	public boolean isUpdate(Context context, Version version) {
		return getCode(getVersion(context)) < getCode(version.getVersion());
	}

	private long getCode(String code) {
		String[] temp = code.replace(".", ",").split(",");
		return temp.length == 2 ? (long) ((Integer.parseInt(temp[0]) * 10000) + (Integer.parseInt(temp[1]) * 100))
				: (long) (((Integer.parseInt(temp[0]) * 10000) + (Integer.parseInt(temp[1]) * 100)) + Integer
						.parseInt(temp[2]));
	}

	public String getVersion(Context context) {
		try {
			return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
		} catch (Exception e) {
			e.printStackTrace();
			return "1.0";
		}
	}
}
